﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.IO;
using System.Configuration;

namespace SteamSwitch
{
    public partial class Form_Main : Sunny.UI.FormMetro
    {
        public Form_Main()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SaveRegSteamUser();
            GetSteamUser();
            GetSteamPath();
        }



        private void button_Login_Click(object sender, EventArgs e)
        {
            SteamAPI.SetAutoLoginUser(cbox_User.Text);
            if (Process.GetProcessesByName("steam").Length > 0)
            {
                if (MessageBox.Show("检测到Steam正在运行,是否强制结束？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    foreach (var item in Process.GetProcessesByName("steam"))
                    {
                        item.Kill();
                    }
                    Process.Start(SteamAPI.GetSteamExePath());
                }
            }
            else
            {
                Process.Start(SteamAPI.GetSteamExePath());
            }
        }
        private void Button_Browse_Click(object sender, EventArgs e)
        {
            ofd_SteamFile.ShowDialog();
        }

        private void button_CreateScript_Click(object sender, EventArgs e)
        {
            //检测Steam.exe是否存在
            if (string.IsNullOrWhiteSpace(Tbox_SteamPath.Text))
            {
                MessageBox.Show("当前路径Steam.exe文件不存在！");
                return;
            }
            //检测Steam用户是否为空
            if (string.IsNullOrWhiteSpace(cbox_User.Text))
            {
                MessageBox.Show("请输入Steam登陆的账号");
                return;
            }
            //构建脚本内容
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(@"@echo off");
            sb.Append(@"@reg add HKCU\SOFTWARE\Valve\Steam /f /v AutoLoginUser /t reg_sz /d ")
                .AppendLine(cbox_User.Text);
            sb.Append(@"start ").Append(Tbox_SteamPath.Text);

            //保存脚本
            sfd_ScriptFile.FileName = cbox_User.Text + ".bat";
            if (sfd_ScriptFile.ShowDialog() == DialogResult.OK)
            {
                File.WriteAllText(sfd_ScriptFile.FileName, sb.ToString());
                MessageBox.Show("保存成功！" + sfd_ScriptFile.FileName);
            }
        }

        private void button_Edit_Click(object sender, EventArgs e)
        {
            Form_UserManage fum = new Form_UserManage();
            if (fum.ShowDialog(this) == DialogResult.OK)
            {
                GetSteamUser();
            }
        }

        #region 一些方法
        private void GetSteamPath()
        {
            if (SteamAPI.GetSteamIsInstall())
            {
                this.Tbox_SteamPath.Text = SteamAPI.GetSteamExePath();
            }
            else
            {
                this.Tbox_SteamPath.Text = "未指到Steam,请安装Steam或者手动指定位置！";
            }
        }

        private void SaveRegSteamUser()
        {
            //检测注册表是否有Steam登陆账户
            if (SteamAPI.GetSteamIsInstall())
            {
                if (!string.IsNullOrWhiteSpace(SteamAPI.GetAutoLoginUser()))
                {
                    Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    if (ConfigurationManager.AppSettings.AllKeys.Count(x => x == SteamAPI.GetAutoLoginUser()) == 0)
                    {
                        conf.AppSettings.Settings.Add(SteamAPI.GetAutoLoginUser(), ""); 
                    }
                    conf.Save(ConfigurationSaveMode.Full);
                }
            }
        }
        private void GetSteamUser()
        {
            cbox_User.Items.Clear();
            //检测配置文件中是否有Steam登陆账户
            Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (conf.AppSettings.Settings.Count > 0)
            {
                foreach (KeyValueConfigurationElement item in conf.AppSettings.Settings)
                {
                    cbox_User.Items.Add(item.Key);
                }
            }
            //处理
            if (cbox_User.Items.Count > 0)
            {
                cbox_User.SelectedItem = SteamAPI.GetAutoLoginUser();
            }
            else
            {
                cbox_User.SelectedIndex = -1;
            }
            
        }
        #endregion
        
    }
}
